﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_QueryFrom_frm_QueryShipmentAffirm : System.Web.UI.Page
{
    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetQueryDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdQueryResult.CurrentPageIndex;
            int intPageCounts = this.dgrdQueryResult.PageCount;

            //判断翻页方向
            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindQueryDataSet(dst, intPageIndex);
        }
    }

    private void GetShipmentAffirmMains(string strParam, string strCurrUserID)
    {
        ClsSellManageDAL cls_SellManage = new ClsSellManageDAL();
        DataSet dst = cls_SellManage.GetSellAffirmDataSet(1, strParam, strCurrUserID, 1, 4);

        if (dst != null)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();

            cls_Property.SetOrGetQueryDataSet = dst;

            this.BindQueryDataSet(dst, 0);
        }
        else
        {
            this.lblQueryRsCounts.Text = "共0条记录";
            this.lblQueryPageCounts.Text = "共0页";
            this.lblQueryCurrPage.Text = "当前第0页";
            this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            this.lblQueryRsCounts.ForeColor = this.lblQueryPageCounts.ForeColor = this.lblQueryCurrPage.ForeColor = Color.FromArgb(172, 168, 153);
        }
    }

    private void BindQueryDataSet(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdQueryResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdQueryResult.DataSource = dst;
            this.dgrdQueryResult.DataBind();

            int intPageCount = this.dgrdQueryResult.PageCount;
            int intCurrPage = this.dgrdQueryResult.CurrentPageIndex + 1;
            this.lblQueryRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblQueryPageCounts.Text = "共" + intPageCount + "页";
            this.lblQueryCurrPage.Text = "当前第" + intCurrPage + "页";
            this.lblQueryRsCounts.Visible = this.lblQueryPageCounts.Visible = this.lblQueryCurrPage.Visible = true;
            this.lbtnQueryFirst.Visible = this.lbtnQueryPrev.Visible = this.lbtnQueryNext.Visible = this.lbtnQueryEnd.Visible = true;
            this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = true;
            this.lblQueryRsCounts.ForeColor = this.lblQueryPageCounts.ForeColor = this.lblQueryCurrPage.ForeColor = Color.FromArgb(78, 78, 78);

            if (intPageCount == 1)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }
            else if (intCurrPage == intPageCount)
            {
                this.lbtnQueryNext.Enabled = this.lbtnQueryEnd.Enabled = false;
            }
            else if (intCurrPage - 1 == 0)
            {
                this.lbtnQueryFirst.Enabled = this.lbtnQueryPrev.Enabled = false;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                string strParam = this.Page.Request.Params["param"];

                this.GetShipmentAffirmMains(strParam, strUserID);
            }
            else
            {
                this.lblJs.Text = "";
            }

            this.imgSubmit.Attributes.Add("OnClick", "window.returnValue=document.getElementById('hidValue').value;window.close();");
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>window.returnValue=-1;window.close();</script>";
        }
    }

    protected void txtKeyValue_TextChanged(object sender, EventArgs e)
    {
        //获取保存的数据集
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetQueryDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            //获取查询字段名和值
            string strValue = ((TextBox)sender).Text.Trim();
            string strKey = this.dropKeyName.SelectedItem.Value;

            //在数据视图中按查询字段名和值进行模糊搜索
            DataGrid dgrd = new DataGrid();
            DataView dvw = dst.Tables[0].DefaultView;

            dvw.RowFilter = strKey + " LIKE '*" + strValue + "*'";

            //绑定数据到数据控件
            this.dgrdQueryResult.CurrentPageIndex = 0;
            this.dgrdQueryResult.DataSource = dvw;
            this.dgrdQueryResult.DataBind();

            this.lblQueryRsCounts.Visible = this.lblQueryPageCounts.Visible = this.lblQueryCurrPage.Visible = false;
            this.lbtnQueryFirst.Visible = this.lbtnQueryPrev.Visible = this.lbtnQueryNext.Visible = this.lbtnQueryEnd.Visible = false;
        }
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            this.GetShipmentAffirmMains("", strUserID);
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>window.returnValue=-1;window.close();</script>";
        }
    }

    protected void dgrdQueryResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Panel panItem = (Panel)e.Item.FindControl("panItem");
            string txt0 = ((TextBox)e.Item.FindControl("txt0")).Text.Trim();
            string txt1 = ((TextBox)e.Item.FindControl("txt1")).Text.Trim();
            string txt2 = ((TextBox)e.Item.FindControl("txt2")).Text.Trim();
            string txt3 = ((TextBox)e.Item.FindControl("txt3")).Text.Trim();
            string txt4 = e.Item.Cells[1].Text.Trim();
            string txt5 = e.Item.Cells[2].Text.Trim();
            string txt6 = e.Item.Cells[3].Text.Trim();
            string txt7 = e.Item.Cells[4].Text.Trim();
            string txt8 = e.Item.Cells[5].Text.Trim();
            string txt9 = e.Item.Cells[6].Text.Trim();
            string txt10 = e.Item.Cells[7].Text.Trim();
            string txt11 = e.Item.Cells[8].Text.Trim();

            string strReturnValue = txt0 + "|" + txt1 + "|" + txt4 + "|" + txt2 + "|" + txt5 + "|" + txt6 + "|" + txt7 + "|" + txt8 + "|" + txt9 + "|" + txt10 + "|" + txt11;

            panItem.Attributes.Add("OnClick", "document.getElementById('hidValue').value='" + strReturnValue + "';");
            panItem.Attributes.Add("OndblClick", "window.returnValue='" + strReturnValue + "';window.close();");
        }
    }
}
